package service

import (
	"advertising/config/redis"
	"advertising/internal/define"
	"context"
	"time"

	"192.168.1.75/go-pkg/errorx"
	"github.com/mojocn/base64Captcha"
)

type Captcha struct {
	CaptchaID  string `json:"captcha_id"`
	CaptchaImg string `json:"captcha_img"`
}

func GetCaptcha() (*Captcha, error) {
	// 验证码数字
	digits := base64Captcha.NewDriverDigit(80, 240, 5, 0.7, 80)
	// 生成base64图片
	store := base64Captcha.DefaultMemStore
	c := base64Captcha.NewCaptcha(digits, store)
	id, base64Img, _, err := c.Generate()
	if err != nil {
		return nil, errorx.New("生成验证码失败", -1)
	}
	rdb := redis.NewDB()
	rdb.Set(context.Background(), define.RedisCaptcha+id, store.Get(id, false), time.Minute) // 缓存验证码内容至redis
	var captcha Captcha
	captcha.CaptchaID = id
	captcha.CaptchaImg = base64Img
	return &captcha, nil
}
